﻿namespace BadHabit.WPFTools.Converters
{
    using System;
    using System.Globalization;
    using System.Windows.Data;

    [ValueConversion(typeof(object), typeof(Int16))]
    public class EnumConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                return Enum.Parse(parameter as System.Type, value.ToString(), true);
            }
            catch
            {
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                return (short)Enum.Parse(parameter as System.Type, value.ToString(), true);
            }
            catch
            {
            }

            return null;
        }
    }
}
